﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
using System.Diagnostics;
using FunctionServices;

namespace SoManyQQNumber
{
    public class QQTool
    {

        private static HttpHelper httpHelper = new HttpHelper();
        /// <summary>
        /// 得到第一次加密后的密码
        /// </summary>
        /// <param name="jsFilePath">js文件</param>
        /// <param name="funcName">加密的方法名</param>
        /// <param name="paramers">加密方法需要传进的参数(一个是密码，另一个是页面上可获取的一个标志码)</param>
        /// <returns>加密过后的密码</returns>
        public static object GetRSAPassword(string jsFilePath, string funcName, params object[] paramers)
        {
            StreamReader reader = new StreamReader(jsFilePath);
            string sScript = reader.ReadToEnd();
            ScriptEngine se = new ScriptEngine(ScriptLanguage.JavaScript);
            object obj = se.Run(funcName, paramers, sScript);
            return obj;
        }

        //"nick=" ＋ name＋ "&isnongli=0&isrunyue=0&appid=9&year=" ＋ year ＋ "&month=" ＋ month ＋ "&day=" ＋ day ＋ "&sex=" ＋ sex＋"&elevel=1&password=" ＋ GetRSAPassword(password) ＋ "&verifycode=" ＋ code;
        public static string RegisterQQNum(string name, string rsapassword, string sex, string year, string month, string day, string code) 
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(name);//编码
            name = System.Text.Encoding.UTF8.GetString(encodedBytes, 0, (int)encodedBytes.Length);
            string registerURL = "http://zc.qq.com/cgi-bin/chs/numreg/get_acc?r=" + GetRandrom();
            string postData = string.Format("nick={0}&isnongli=0&isrunyue=0&appid=9&year={1}&month={2}&day={3}&sex={4}&elevel=1&password={5}&verifycode={6}", "aaaaabbbb", year, month, day, sex, rsapassword, code);

            Debug.WriteLine(postData);
           // httpHelper.CC = new System.Net.CookieContainer(); 
            string html = httpHelper.PostAndGetHtml(registerURL, postData, null, null, false, Encoding.UTF8);

            //string html= htlp.GetHtml(registerURL, postData, Encoding.UTF8);
            return html;

        }


        public static string GetRandrom() 
        {
            Random rand = new Random();
            string strNum = "";
            int i = 0;
            while (i<16)
            {
                i++;
                int randomNum = rand.Next(0,10);
                strNum += randomNum.ToString();
            }
            strNum = "0." + strNum;
            return strNum;
        }


        public static Image GetCode() 
        {
            string path = "http://captcha.qq.com/getimage?aid=1007901&r="+GetRandrom();
            Image image= httpHelper.GetAndGetBitmap(path, null, null, false);
            return image;
             
        }


        public static string GetAddress() 
        {
            string path = "http://zc.qq.com/cgi-bin/iframe/numreg/init_9?id="+ GetRandrom();
            string html= httpHelper.GetAndGetHtml(path, null, null, true, Encoding.UTF8);
            return html;
        }
    }
}
